const myInstanceof = require("./template.js")

function testInstanceof() {
  console.log(
    "1. 基本数组类型:",
    (() => {
      const arr = [1, 2, 3]
      return (
        myInstanceof(arr, Array) === true &&
        myInstanceof(arr, Object) === true &&
        myInstanceof(arr, Function) === false
      )
    })()
  )

  console.log(
    "2. 基本对象类型:",
    (() => {
      const obj = { a: 1 }
      return (
        myInstanceof(obj, Object) === true &&
        myInstanceof(obj, Array) === false &&
        myInstanceof(obj, Function) === false
      )
    })()
  )

  console.log(
    "3. 函数类型:",
    (() => {
      const fn = function () {}
      return (
        myInstanceof(fn, Function) === true &&
        myInstanceof(fn, Object) === true &&
        myInstanceof(fn, Array) === false
      )
    })()
  )

  console.log(
    "4. 字符串包装对象:",
    (() => {
      const str = new String("hello")
      return (
        myInstanceof(str, String) === true &&
        myInstanceof(str, Object) === true &&
        myInstanceof("hello", String) === false
      ) // 原始字符串
    })()
  )

  console.log(
    "5. 数字包装对象:",
    (() => {
      const num = new Number(42)
      return (
        myInstanceof(num, Number) === true &&
        myInstanceof(num, Object) === true &&
        myInstanceof(42, Number) === false
      ) // 原始数字
    })()
  )

  console.log(
    "6. 布尔包装对象:",
    (() => {
      const bool = new Boolean(true)
      return (
        myInstanceof(bool, Boolean) === true &&
        myInstanceof(bool, Object) === true &&
        myInstanceof(true, Boolean) === false
      ) // 原始布尔值
    })()
  )

  console.log(
    "7. 日期对象:",
    (() => {
      const date = new Date()
      return (
        myInstanceof(date, Date) === true &&
        myInstanceof(date, Object) === true &&
        myInstanceof(date, Array) === false
      )
    })()
  )

  console.log(
    "8. 正则表达式:",
    (() => {
      const regex = /test/g
      return (
        myInstanceof(regex, RegExp) === true &&
        myInstanceof(regex, Object) === true &&
        myInstanceof(regex, Function) === false
      )
    })()
  )

  console.log(
    "9. 自定义构造函数:",
    (() => {
      function Person(name) {
        this.name = name
      }
      const person = new Person("Alice")
      return (
        myInstanceof(person, Person) === true &&
        myInstanceof(person, Object) === true &&
        myInstanceof(person, Function) === false
      )
    })()
  )

  console.log(
    "10. 继承链测试:",
    (() => {
      function Animal(name) {
        this.name = name
      }
      function Dog(name, breed) {
        Animal.call(this, name)
        this.breed = breed
      }
      Dog.prototype = Object.create(Animal.prototype)
      Dog.prototype.constructor = Dog

      const dog = new Dog("Buddy", "Golden Retriever")
      return (
        myInstanceof(dog, Dog) === true &&
        myInstanceof(dog, Animal) === true &&
        myInstanceof(dog, Object) === true
      )
    })()
  )

  console.log(
    "11. 原始类型测试:",
    (() => {
      return (
        myInstanceof(null, Object) === false &&
        myInstanceof(undefined, Object) === false &&
        myInstanceof(42, Number) === false &&
        myInstanceof("hello", String) === false &&
        myInstanceof(true, Boolean) === false
      )
    })()
  )

  console.log(
    "12. 错误对象类型:",
    (() => {
      const error = new Error("test error")
      return (
        myInstanceof(error, Error) === true &&
        myInstanceof(error, Object) === true &&
        myInstanceof(error, Array) === false
      )
    })()
  )

  console.log(
    "13. Promise对象:",
    (() => {
      const promise = Promise.resolve(42)
      return (
        myInstanceof(promise, Promise) === true &&
        myInstanceof(promise, Object) === true &&
        myInstanceof(promise, Function) === false
      )
    })()
  )

  console.log(
    "14. Map和Set对象:",
    (() => {
      const map = new Map()
      const set = new Set()
      return (
        myInstanceof(map, Map) === true &&
        myInstanceof(set, Set) === true &&
        myInstanceof(map, Object) === true &&
        myInstanceof(set, Object) === true
      )
    })()
  )

  console.log(
    "15. 箭头函数:",
    (() => {
      const arrowFn = () => {}
      return (
        myInstanceof(arrowFn, Function) === true &&
        myInstanceof(arrowFn, Object) === true
      )
    })()
  )

  console.log(
    "16. 类定义测试:",
    (() => {
      class MyClass {
        constructor(value) {
          this.value = value
        }
      }
      const instance = new MyClass(42)
      return (
        myInstanceof(instance, MyClass) === true &&
        myInstanceof(instance, Object) === true &&
        myInstanceof(instance, Function) === false
      )
    })()
  )

  console.log(
    "17. 类继承测试:",
    (() => {
      class Parent {
        constructor(name) {
          this.name = name
        }
      }
      class Child extends Parent {
        constructor(name, age) {
          super(name)
          this.age = age
        }
      }
      const child = new Child("Alice", 10)
      return (
        myInstanceof(child, Child) === true &&
        myInstanceof(child, Parent) === true &&
        myInstanceof(child, Object) === true
      )
    })()
  )

  console.log(
    "18. Object.create测试:",
    (() => {
      function Constructor() {}
      const obj = Object.create(Constructor.prototype)
      return (
        myInstanceof(obj, Constructor) === true &&
        myInstanceof(obj, Object) === true
      )
    })()
  )

  console.log(
    "19. 原型链被修改:",
    (() => {
      function Custom() {}
      const obj = new Custom()

      // 验证正常情况
      const normalResult = myInstanceof(obj, Custom)

      // 修改原型链
      Custom.prototype = {}
      const modifiedResult = myInstanceof(obj, Custom)

      return normalResult === true && modifiedResult === false
    })()
  )

  console.log(
    "20. null原型对象:",
    (() => {
      const obj = Object.create(null)
      return (
        myInstanceof(obj, Object) === false &&
        myInstanceof(obj, Array) === false
      )
    })()
  )

  console.log(
    "21. 构造函数参数验证:",
    (() => {
      const obj = {}
      try {
        // 非函数作为构造函数参数
        return (
          myInstanceof(obj, {}) === false ||
          myInstanceof(obj, null) === false ||
          myInstanceof(obj, undefined) === false
        )
      } catch (e) {
        // 如果抛出错误也是合理的
        return true
      }
    })()
  )

  console.log(
    "22. Symbol测试:",
    (() => {
      const sym = Symbol("test")
      return (
        myInstanceof(sym, Symbol) === false &&
        myInstanceof(sym, Object) === false
      )
    })()
  )

  console.log(
    "23. BigInt测试:",
    (() => {
      const bigint = BigInt(123)
      return (
        myInstanceof(bigint, BigInt) === false &&
        myInstanceof(bigint, Object) === false
      )
    })()
  )

  console.log(
    "24. 复杂继承链:",
    (() => {
      function GrandParent() {}
      function Parent() {}
      function Child() {}

      Parent.prototype = Object.create(GrandParent.prototype)
      Child.prototype = Object.create(Parent.prototype)

      const instance = new Child()
      return (
        myInstanceof(instance, Child) === true &&
        myInstanceof(instance, Parent) === true &&
        myInstanceof(instance, GrandParent) === true &&
        myInstanceof(instance, Object) === true
      )
    })()
  )

  console.log(
    "25. 与原生instanceof对比:",
    (() => {
      const arr = [1, 2, 3]
      const obj = { a: 1 }
      const fn = function () {}

      return (
        myInstanceof(arr, Array) === arr instanceof Array &&
        myInstanceof(obj, Object) === obj instanceof Object &&
        myInstanceof(fn, Function) === fn instanceof Function &&
        myInstanceof(arr, Object) === arr instanceof Object
      )
    })()
  )

  console.log(
    "26. this上下文测试:",
    (() => {
      const obj = {
        items: [1, 2, 3],
        checkType: function(Constructor) {
          return myInstanceof(this.items, Constructor)
        }
      }
      return obj.checkType(Array) === true
    })()
  )

  console.log(
    "27. 方法调用this绑定:",
    (() => {
      const obj = {
        data: { a: 1 },
        checkObjectType: function() {
          return myInstanceof(this.data, Object)
        }
      }
      return obj.checkObjectType() === true
    })()
  )

  console.log(
    "28. 复杂对象this绑定:",
    (() => {
      const obj = {
        config: { Constructor: Array },
        checkWithConfig: function(value) {
          return myInstanceof(value, this.config.Constructor)
        }
      }
      return obj.checkWithConfig([1, 2, 3]) === true
    })()
  )
}

testInstanceof()
